#ifndef __HTTP_SERVER_H__
#define __HTTP_SERVER_H__

#include "tcp_server.h"
#include "http_session.h"
#include "servlet.h"


namespace johnsonli
{
namespace http
{
    /**
     * @brief HTTP服务器类
     */
    class HttpServer : public TcpServer 
    {
    public:
        typedef std::shared_ptr<HttpServer> ptr;

        /**
         * @brief 构造函数
         * @param[in] keepalive 是否长连接
         * @param[in] worker 工作调度器
         * @param[in] accept_worker 接收连接调度器
         */
        HttpServer(bool keepalive = false
                ,const std::string& servername = "johnsonli/1.0.0"
                ,johnsonli::IOManager* worker = johnsonli::IOManager::GetThis()
                ,johnsonli::IOManager* io_worker = johnsonli::IOManager::GetThis()
                ,johnsonli::IOManager* accept_worker = johnsonli::IOManager::GetThis());
                

        virtual void setName(const std::string& v) override;

        ServletDispatch::ptr getServletDispatch() const { return m_dispatch;}
        void setServletDispatch(ServletDispatch::ptr v) { m_dispatch = v;}
    
    protected:
        virtual void handleClient(Socket::ptr client) override;

        void doWorker(johnsonli::http::HttpRequest::ptr request
            , johnsonli::http::HttpResponse::ptr response
            , johnsonli::http::HttpSession::ptr session);

        void doSend(johnsonli::http::HttpSession::ptr session, johnsonli::http::HttpResponse::ptr response);

    private:
        bool m_isKeepalive;                 // 是否支持长连接
        ServletDispatch::ptr m_dispatch;    // Servlet分发器
    };
}
} 


#endif